jQuery.noConflict();
//jquery extend
(function(jQuery) {
	jQuery.extend(jQuery.fn, {
		outer : function() {
			return jQuery(jQuery("<div></div>").html(this.clone())).html();
		}
	});
	jQuery
			.extend({
				// 弹出浮层
				popMsg : function(m, title, width, height) {
					var t = title || "标题";
					var w = width || 400;
					var h = height || 300;
					h = parseInt(h) + 22;//算上标题
					var str = '<div class="pop-title"><strong>'
							+ title
							+ '</strong>&nbsp;<a title="关闭" class="close" href="javascript:void(0);" onclick="jQuery.unblockUI();"></a></div>';
					var mes = jQuery('<div class="pop-body"></div>').html(
							str + jQuery(m).clone().css("display", "").outer());
					var left = 0;
					var top = 0;
					if (jQuery.browser.msie) {
						/*
						 * for ie
						 */
						left = (document.body.clientWidth - w) / 2;
						top = (document.documentElement.clientHeight - h) / 2
								+ document.body.scrollTop;
						// alert(left + ':' + top);
					} else {
						/*
						 * for firefox
						 */
						left = (window.innerWidth - w) / 2 + window.pageXOffset;
						top = (window.innerHeight - h) / 2;
					}
					jQuery.blockUI({
						message : mes,
						css : {
							left : left + 'px',
							top : top + 'px',
							width : w + 'px',
							height : h + 'px',
							cursor : 'hand'
						},
						overlayCSS : {
							cursor : 'default'
						}
					});
					// jQuery('.blockOverlay').append('<img
					// id="buttonCloseBlockUI" src="' + page.basePath +
					// 'images/buttonCLOSE.gif" alt="close"
					// onclick="jQuery.unblockUI()" style="float:right;margin:30
					// 10 0 0;cursor:hand;">');

					jQuery('.blockOverlay').attr('title', '点击这里关闭弹出窗口').click(
							jQuery.unblockUI);
				},
				popUrl : function(url, title, width, height){
					var w = width || 800;
					var h = height || 400;
					title = title || "";
					var html = "";
					html += '<div>';
					html += '	<iframe width="'+(w)+'" height="'+(h)+'" frameborder="0" border="0" style="width:100%;" onload="loadFrameUrl('+"'"+url+"'"+', this)"></iframe>';
					html += '</div>';
					jQuery.popMsg(html, title, w, h);
				},
				hiddenRequest : function(url){
					if(!confirm("确认要执行该操作吗？")) return;
					var html = "";
					html += '<div style="display:none;">';
					html += '	<iframe src="'+url+'"></iframe>';
					html += '</div>';
					jQuery("body").append(html);
				}
			});
})(jQuery);
function loadFrameUrl(url, frame){
	if(frame.src == ""){
		window["__currentPop"] = frame;
		setTimeout('window["__currentPop"].src = "'+url+'"', 0);
	}
}
function initDropList(){
	var args = Array.prototype.slice.call(arguments);
	if(args.length < 3) return;
	var parentName = args.shift();
	var hiddenName = args.shift();
	var data = args.shift();
	var isRoot = true;
	var showBack = false;
	if( parentName != null ){
		isRoot = false;
		var parentInput = jQuery("input[name='"+parentName+"']")[0];
		if(parentInput != null && parentInput.value != null && parentInput.value.length > 0){
			showBack = true;
			var dataNew = [];
			for(var i = 0; i < data.length; i++){
				var p = data[i];
				if(parentInput.value == p[2]){
					dataNew.push(p);
				}
			}
			data = dataNew;
		}
	}
	
	var selectId = "select_" + hiddenName;
	var hiddenInput = jQuery("input[name='"+hiddenName+"']")[0];
	if(hiddenInput == null || data == null || data.length == null) return;
	jQuery(_g(selectId)).find(" option[class!='default-option']").remove();
	//jQuery("#"+selectId+" option[class!='default-option']").remove();
	if(isRoot || showBack){
		var html = "";
		for(var i = 0; i < data.length; i++){
			var p = data[i];
			html += '<option value="'+p[0]+'"';
			if(hiddenInput.value == p[0])
				html += ' selected';
			html += '>'+p[1]+'</option>';
		}
		jQuery(_g(selectId)).append(html);
		jQuery(_g(selectId)).change(function(){
			changeDropList.apply(null,args);
			hiddenInput.value = this.options[this.selectedIndex].value;
			initDropList.apply(null,args);
		});
	}
	initDropList.apply(null,args);
}
function changeDropList(){
	var args = Array.prototype.slice.call(arguments);
	if(args.length < 3) return;
	var parentName = args.shift();
	var hiddenName = args.shift();
	var data = args.shift();
	var selectId = "select_" + hiddenName;
	var hiddenInput = jQuery("input[name='"+hiddenName+"']")[0];
	jQuery(_g(selectId)).find(" option[class!='default-option']").remove();
	//jQuery("#"+selectId+" option[class!='default-option']").remove();
	hiddenInput.value = "";
	changeDropList.apply(null,args);
}


function createPcd(provinceName,cityName,districtName){
	initDropPcdList(null,provinceName,window.pcd.province,provinceName,cityName,window.pcd.city,cityName,districtName,window.pcd.district);
}
function initDropPcdList(){
	var args = Array.prototype.slice.call(arguments);
	if(args.length < 3) return;
	var parentName = args.shift();
	var hiddenName = args.shift();
	var data = args.shift();
	var isRoot = true;
	var showBack = false;
	var isCitySel=false;
	var isDistSel=false;
	if(args.length==3)
		isCitySel=true;
	else if(args.length==0)
		isDistSel=true;
	if( parentName != null ){
		isRoot = false;
		var parentInput = jQuery("input[name='"+parentName+"']")[0];
		if(parentInput != null && parentInput.value != null && parentInput.value.length > 0){
			showBack = true;
			var dataNew = [];
			var parentValue=parentInput.value;
			if((isDistSel)&&(parentValue=="55"||parentValue=="56"||parentValue=="58"||parentValue=="59")){
				parentValue=parentValue+"_"+parentValue;
			}				
			for(var i = 0; i < data.length; i++){
				var p = data[i];				
				if(parentValue== p[2]){
					dataNew.push(p);
				}
			}
			data = dataNew;
		}
	}
	
	var selectId = "select_" + hiddenName;
	var hiddenInput = jQuery("input[name='"+hiddenName+"']")[0];
	if(hiddenInput == null || data == null || data.length == null) return;
	jQuery(_g(selectId)).find(" option[class!='default-option']").remove();
	//jQuery("#"+selectId+" option[class!='default-option']").remove();
			
	if(isRoot || showBack){
		var html = "";
		var hiddenValue=hiddenInput.value;		
		if((isCitySel)&&(hiddenValue=="55"||hiddenValue=="56"||hiddenValue=="58"||hiddenValue=="59"))
			hiddenValue=hiddenValue+"_"+hiddenValue;
		for(var i = 0; i < data.length; i++){
			var p = data[i];
			html += '<option value="'+p[0]+'"';
			
			if(hiddenValue == p[0])
				html += ' selected';
			html += '>'+p[1]+'</option>';
		}
		jQuery(_g(selectId)).append(html);
		jQuery(_g(selectId)).change(function(){
			changeDropList.apply(null,args);
			
			if(this.options[this.selectedIndex].value=="55_55"||this.options[this.selectedIndex].value=="56_56"
				||this.options[this.selectedIndex].value=="58_58"||this.options[this.selectedIndex].value=="59_59"){
				hiddenInput.value=this.options[this.selectedIndex].value.split("_")[0];
			}else
				hiddenInput.value = this.options[this.selectedIndex].value;
			initDropPcdList.apply(null,args);
		});
	}
	initDropPcdList.apply(null,args);
}
